﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor.app
{
    /// <summary>
    /// RImageView.xaml 的交互逻辑
    /// </summary>
    public partial class RImageView : UserControl
    {
        public Action<int, int, int> OnClickFrameCalback;
        public Action<int[]> OnClickFrameListCallback;

        const int CENTRE_DOT_SIZE = 9;

        comm.objst.RImage _rImage = null;

        public RImageView()
        {
            InitializeComponent();

            brL.MouseMove += new MouseEventHandler(brL_MouseMove);
            brL.MouseLeftButtonDown += new MouseButtonEventHandler(brL_MouseLeftButtonDown);

            brT.MouseMove += new MouseEventHandler(brT_MouseMove);
            brT.MouseLeftButtonDown += new MouseButtonEventHandler(brT_MouseLeftButtonDown);
        }

        void brT_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_rImage != null && OnClickFrameListCallback != null)
            {
                var pt = e.GetPosition(this);
                var list = new List<int>();
                for (int i = 0, len = _rImage.FrameList.Count; i < len; i++)
                {
                    var frame = _rImage.FrameList[i];
                    if (frame.X <= pt.X && frame.X + frame.Width >= pt.X)
                    {
                        list.Add(i);
                    }
                }
                OnClickFrameListCallback(list.ToArray());
            }
        }
        void brL_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_rImage != null && OnClickFrameListCallback != null)
            {
                var pt = e.GetPosition(this);
                var list = new List<int>();
                for (int i = 0, len = _rImage.FrameList.Count; i < len; i++)
                {
                    var frame = _rImage.FrameList[i];
                    if (frame.Y <= pt.Y && frame.Y + frame.Height >= pt.Y)
                    {
                        list.Add(i);
                    }
                }
                OnClickFrameListCallback(list.ToArray());
            }
        }
        void brT_MouseMove(object sender, MouseEventArgs e)
        {
            if (OnClickFrameListCallback != null)
            {
                var pt = e.GetPosition(this);
                lineV.Margin = new Thickness(pt.X, lineV.Margin.Top, 0, 0);
            }
        }
        void brL_MouseMove(object sender, MouseEventArgs e)
        {
            if (OnClickFrameListCallback != null)
            {
                var pt = e.GetPosition(this);
                lineH.Margin = new Thickness(lineH.Margin.Left, pt.Y, 0, 0);
            }
        }

        public void Load(comm.objst.RPack rpack, string imageResKey, int[] masks = null)
        {
            _rImage = null;

            var image = rpack.GetResource<comm.objst.RImage>(imageResKey);
            if (image == null || image.ImageBytes == null)
            {
                return;
            }
            _rImage = image;
            var maskList = new List<int>();
            if (masks != null)
            {
                maskList.AddRange(masks);
            }

            var bitmap = new BitmapImage();
            bitmap.FromBytes(image.ImageBytes);

            cvImage.Background = new ImageBrush { ImageSource = bitmap };
            cvImage.Width = bitmap.PixelWidth;
            cvImage.Height = bitmap.PixelHeight;

            gridRoot.Width = cvImage.Width + 20;
            gridRoot.Height = cvImage.Height + 20;

            cvImage.Children.Clear();
            var ix = 0;
            foreach (var frame in image.FrameList)
            {
                var grid = new Grid
                {
                    Tag = ix,
                    Width = frame.Width,
                    Height = frame.Height,
                    Background = new SolidColorBrush(Color2.Transparent())
                };
                grid.SetLeftTop(frame.X, frame.Y);
                grid.Children.Add(new Border
                {
                    Child = new Border
                    {
                        BorderBrush = new SolidColorBrush(Color2.White(20)),
                        BorderThickness = new Thickness(1, 1, 0, 0)
                    },
                    BorderBrush = new SolidColorBrush(Color2.Black(50)),
                    BorderThickness = new Thickness(0, 0, 1, 1)
                });
                if (maskList.Contains(ix))
                {
                    grid.Children.Add(new Border { Background = new SolidColorBrush(Color2.OrangeRed(30)) });
                }
                grid.Children.Add(new TextBlock { Text = ix.ToString(), FontSize = 12, Margin = new Thickness(1, 1, 0, 0), Foreground = new SolidColorBrush(Color2.White(50)) });
                grid.Children.Add(new TextBlock { Text = ix.ToString(), FontSize = 12 });

                grid.Children.Add(new Ellipse
                {
                    Opacity = .6,
                    Width = CENTRE_DOT_SIZE,
                    Height = CENTRE_DOT_SIZE,
                    Fill = new SolidColorBrush(Color2.Red()),
                    Stroke = new SolidColorBrush(Color2.White()),
                    StrokeThickness = 1,
                    Margin = new Thickness(frame.CentreX - CENTRE_DOT_SIZE / 2, frame.CentreY - CENTRE_DOT_SIZE / 2, 0, 0),
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top
                });

                grid.MouseLeftButtonDown += new MouseButtonEventHandler(grid_MouseLeftButtonDown);

                cvImage.Children.Add(grid);
                ix++;
            }
        }

        void grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var grid = (Grid)sender;
            var ix = (int)grid.Tag;
            if (OnClickFrameCalback != null)
            {
                var pt = e.GetPosition(grid);
                OnClickFrameCalback(ix, (int)pt.X, (int)pt.Y);
            }
        }
    }
}
